var common = require('../../../utils/common.js');
var app = getApp();
var baseUrl = app.url;
var sliderWidth = 96; 

Page({
    data: {
        tabs: ["通用信息", "智能设备", "电子文件"],
        activeIndex: 0,
        sliderOffset: 0,
        sliderLeft: 0,
        b_index:0,
        car_breed:['小型轿车','SUV','客车'],
        breed_key:0,
        multiArray:[],
        multiIndex: [0, 0],
        relevance:["请选择","分时租赁","企业用车","共享用车","长租车辆","自测试用","上牌整备","整备待租"],
        relev_id:0,
        agency_id:0,
        supplier:[{id:"",name:"请选择"},{id:1376920002,name:"博鹭途"},{id:1376920001,name:"唯特迅"},{id:1376920003,name:"博鹭途(IOT)"}],
        supp_id:0,
        b_supp_id:0,
        files:[[],[],[]]
    },
    onLoad: function () {
        var that = this;
        wx.getSystemInfo({
            success: function(res) {
                that.setData({
                    sliderLeft: (res.windowWidth / that.data.tabs.length - sliderWidth) / 2,
                    sliderOffset: res.windowWidth / that.data.tabs.length * that.data.activeIndex
                });
            }
        });
        common.bestCurl('GET','/cars/add',res=>{
            if(res.statusCode == 200){
                if(res.data.length!=0){
                array = [
                            [{id:0,name:'公司'},{id:1,name:'个人'}],
                            [{id: 1, name: "广州市宝奕瀛新汽车有限责任公司"},
                             {id: 3, name: "广州晶意新能源汽车租赁有限公司"},
                             {id: 2, name: "广州海印蔚蓝新能源科技有限公司"},
                             {id: 4, name: "深圳宝德新能源汽车租赁有限公司"}
                            ]
                        ];
                // console.log(array[1][0])
                    multiArray = [[{id:0,name:'公司'},{id:1,name:'个人'}]];
                    multiArray[1] = res.data.holder;
                    console.log(multiArray[0][1].name,multiArray[1][1].name)
                    this.setData({brand_id:res.data.brand,multiArray:multiArray,agency:res.data.agency})
                }
            }
        },'../../')
    },
    tabClick: function (e) {
        this.setData({
            sliderOffset: e.currentTarget.offsetLeft,
            activeIndex: e.currentTarget.id
        });
    },
    bindMultiPickerColumnChange:function(){

    },
    bindBrandChange: function (e) {
        this.setData({
            b_index: e.detail.value
        })
    },   
    bindBreedChange: function (e) {
        this.setData({
            breed_key: e.detail.value
        })
    },
    bindMultiPickerChange: function (e) {
        console.log(e.detail.value);
        this.setData({
          multiIndex: e.detail.value
        })
    }, 
    bindRelevChange:function (e) {
        this.setData({
            relev_id: e.detail.value
        })
    },
    agencyChange:function (e) {
       this.setData({
            agency_id: e.detail.value
        })
    }, 
    bindSuppChange:function (e) {
        this.setData({
            supp_id: e.detail.value
        })
    }, 
    bindBsuppChange:function (e) {
        this.setData({
            b_supp_id: e.detail.value
        })
    },
    chooseImage: function (e) {
        var file_type = e.target.id;
        var that = this;
        wx.chooseImage({
            sizeType: ['original', 'compressed'],
            sourceType: ['album', 'camera'],
            success: function (res) {
                let files= that.data.files;         
                let fileArr = files[file_type].concat(res.tempFilePaths)
                files[file_type] = new Object;
                files[file_type] = fileArr;
                that.setData({
                    files:files
                });
            }
        })
    },
    previewImage: function(e){
        var file_type = e.currentTarget.id
        wx.previewImage({
            current: e.currentTarget.dataset.url,
            urls: this.data.files[file_type]
        })
    },
    clickWrongIcon:function(event){
        var type = event.currentTarget.dataset.type;
        id = event.target.id;
        let files= this.data.files;
        files[type].splice(id,1)
        this.setData({files:files})
    },
    formSubmit:function (event) {
        var formData = event.detail.value;
        let appData = this.data;
        let data = [];
        data['holder_type'] = appData.multiArray[0][appData.multiIndex[0]].id
        data['holder_id'] = appData.multiArray[1][appData.multiIndex[1]].id;
        data['brand_id'] = appData.brand_id[appData.b_index].id;
        data['agency_id'] = appData.agency[appData.agency_id].id;
        data['supplier_no'] = appData.supplier[appData.supp_id].id;
        data['back_supplier_no'] = appData.supplier[appData.b_supp_id].id;
        if(formData.car_unique_id.length == 0){
            wx.showModal({
                content: '请添加车架号',
                showCancel: false,
            });  
        }else{
            common.bestCurl('POST','/cars/add',res=>{
                if(res.statusCode == 200){
                    if(res.data.code == 1){
                        var id =res.data.id;
                        var filesUrl =this.data.files;
                        var flag = 0;
                        var filesNum = filesUrl[0].length + filesUrl[1].length + filesUrl[2].length;
                        console.log(filesUrl);
                        if(filesUrl[0].length>0 || filesUrl[1].length>0 || filesUrl[2].length>0){
                            wx.showLoading({
                                title: '图片上传中',
                                icon: 'loading',
                                mask:true
                            });
                            let SESSID = wx.getStorageSync('PHPSESSID');
                            if(SESSID){
                                var PHPSESSID = '?PHPSESSID='+SESSID;
                            }else{
                                PHPSESSID = '?';
                            }
                            url=baseUrl+'/cars/add_file'+PHPSESSID;

                            for (let travel of filesUrl[0]) {
                                wx.uploadFile({
                                    url: url,
                                    filePath: travel,
                                    name: 'uploadfile',
                                    formData:{
                                        "id": id,
                                        "type":0
                                    },
                                    success: function(res){                        
                                        flag++;
                                        if(filesNum == flag){
                                            wx.hideLoading();
                                            wx.showToast({
                                                title: '添加成功',
                                                icon: '',
                                                duration: 5000
                                            });
                                            setTimeout(res=>{
                                                wx.redirectTo({
                                                    url: '../list/list'
                                                });
                                            },2000);
                                        }   
                                    }
                                });
                            }
                            for (let register of filesUrl[1]) {
                                wx.uploadFile({
                                    url: url,
                                    filePath: register,
                                    name: 'uploadfile',
                                    formData:{
                                        "id": id,
                                        "type":1
                                    },
                                    success: function(res){                        
                                        flag++;
                                        if(filesNum == flag){
                                            wx.hideLoading();
                                            wx.showToast({
                                                title: '添加成功',
                                                icon: '',
                                                duration: 5000
                                            });
                                            setTimeout(res=>{
                                                wx.redirectTo({
                                                    url: '../list/list'
                                                });
                                            },2000);
                                        }   
                                    }
                                });
                            }
                            for (let image of filesUrl[2]) {
                                wx.uploadFile({
                                    url: url,
                                    filePath: image,
                                    name: 'uploadfile',
                                    formData:{
                                        "id": id,
                                        "type":2
                                    },
                                    success: function(res){                        
                                        flag++;
                                        if(filesNum == flag){
                                            wx.hideLoading();
                                            wx.showToast({
                                                title: '添加成功',
                                                icon: '',
                                                duration: 5000
                                            });
                                            setTimeout(res=>{
                                                wx.redirectTo({
                                                    url: '../list/list'
                                                });
                                            },2000);
                                        }   
                                    }
                                });
                            }                           
                        }else{
                            wx.showToast({
                                title: '添加成功',
                                icon: '',
                                duration: 5000
                            });
                            setTimeout(res=>{
                                wx.redirectTo({
                                    url: '../list/list'
                                })
                            },2000)
                        }
                    }else if(res.data.code == 2){
                        wx.showModal({
                            content: res.data.msg,
                            showCancel: false,
                        });
                    }else{
                        wx.showModal({
                            content: "添加失败,请稍后再试",
                            showCancel: false,
                        });
                    }
                }
            },Object.assign(formData,data),'../../')            
        }
    }
});

